﻿using blackenter.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackenter.Business.Sites {
    public class CmsSite : ISite {

        #region Fields

        private ISiteDbContext dbContext;

        #endregion

        #region Properties

        public int SiteId {
            get;
            private set;
        }

        public string SiteName {
            get;
            set;
        }

        public bool Exists {
            get;
            private set;
        }

        #endregion

        #region Constructors

        public CmsSite() {
            this.dbContext = DbFactory.GetSiteDbContext();
        }

        public CmsSite(int siteId)
            : this() {
            this.ReadSite(siteId);
        }

        #endregion

        #region Methods

        private void ReadSite(int siteId) {
            SiteData data = this.dbContext.GetSiteBySiteId(siteId);
            if (!data.Id.Equals(siteId)) return;

            this.SiteId = data.Id;
            this.SiteName = data.Name;
            this.Exists = true;
        }

        public bool Save() {
            if (this.SiteId == 0) {
                int siteId = this.dbContext.SaveNewSite(this.SiteName);
                if (siteId > 0) {
                    this.SiteId = siteId;
                    this.Exists = true;
                    return true;
                } else
                    return false;
            } else {
                return this.dbContext.SaveSite(this.SiteId, this.SiteName);
            }
        }

        #endregion

    }
}
